$(document).ready(function() {
	var lastsel2;
	$("#viewReceiptsTable").jqGrid({
		url : 'rec/viewReceiptDetails',
		datatype : "json",
		colNames : ['ID', 'Name', 'Receipt Date', 'Total', 'Retailer', 'Receipt Category', 'Image'],
		colModel : [{
			name : 'id',
			index : 'id',
			width : 70,
			editable : false,
			editoptions : {
				readonly : true,
				size : 10
			},
			hidden : true
		}, {
			name : 'name',
			index : 'name',
			width : 60,
			formatter : receiptEditLinkFormatter,
			editoptions : {
				maxlength : "30"
			}
		}, {
			name : 'receiptDate',
			index : 'receiptDate',
			width : 60,
			datefmt : 'dd/mm/yyyy',
			formatter : 'date',
			formatter : receiptDateFormatter
		}, {
			name : 'total',
			index : 'total',
			width : 60,
			edittype : 'text',
			formatter : 'number',
			editoptions : {
				maxlength : "30"
			}
		}, {
			name : 'retailer.value',
			index : 'retailer.value',
			width : 60,
			editable : true,
			edittype : 'text',
			editoptions : {
				maxlength : "30"
			}
		}, {
			name : 'receiptCategory.value',
			index : 'receiptCategory',
			width : 60,
			editable : true,
			edittype : 'text',
			editoptions : {
				maxlength : "30"
			}
		}, {
			name : 'receiptUpload.receiptURL',
			index : 'receiptUpload.receiptURL',
			/* datefmt: 'dd/mm/yyyy', */
			formatter : receiptImageFormatter,
			width : 10,
			align : 'center',
			editrules : {
				date : true
			}
		}],
		pager : '#pager',
		rowNum : 10,
		rowList : [10, 20, 30],
		sortname : 'id',
		width : 800,
		height : 'auto',
		sortorder : 'desc',
		viewrecords : true,
		emptyrecords : 'No Records found',
		gridview : true,
		jsonReader : {
			repeatitems : false
		},
		ondblClickRow : function(rowid, iRow, iCol, e) {
			openReceiptEditPage(rowid);
		},
		caption : 'Receipt details'
	});

	$("#viewReceiptsTable").jqGrid('navGrid', '#pager', {
		edit : false,
		add : false,
		del : true,
		search : false
	}, {}, {}, {
		url : 'rec/deleteReceipt'
	});

});
function receiptImageFormatter(cellvalue, options, rowObject) {
	return "<img src='" + cellvalue + "', alt='" + cellvalue + "', title='image title hi' height=40, width=40></img>";
}

function openReceiptEditPage(receiptId) {
	application.loadPage("rec/loadReceiptsEditPage/" + receiptId);
}

function receiptEditLinkFormatter(el, cellval, opts) {
	return "<a href='#' onClick='javascript:openReceiptEditPage()'>" + cellval + "</a>";

}

function receiptDateFormatter(cellvalue, options, rowObject) {
	return new Date(cellvalue);
}

function receiptEditLinkFormatter(cellValue, options, rowdata, action) 
{
	return "<a href='#' onClick='javascript:openReceiptEditPage("+cellValue+")'>" + cellValue + "</a>";
} 